Product Code Database
Example Keywords: pokimon -software $16
   » » Wiki: Tridiagonal Matrix Algorithm
Tag Wiki 'Tridiagonal Matrix Algorithm'.
Tag

Tridiagonal matrix algorithm
 (

 C O N T E N T S 

In numerical linear algebra, the tridiagonal matrix algorithm, also known as the Thomas algorithm (named after ), is a simplified form of Gaussian elimination that can be used to solve tridiagonal systems of equations. A tridiagonal system for n unknowns may be written as

a_i x_{i-1} + b_i x_i + c_i x_{i+1} = d_i,
where a_1 = 0 and c_n = 0.

\begin{bmatrix}
  b_1 & c_1 &        &        &  0      \\
  a_2 & b_2 & c_2    &        &         \\
      & a_3 & b_3    & \ddots &         \\
      &     & \ddots & \ddots & c_{n-1} \\
  0   &     &        & a_n    & b_n
     
\end{bmatrix} \begin{bmatrix}
  x_1    \\
  x_2    \\
  x_3    \\
  \vdots \\
  x_n
     
\end{bmatrix} = \begin{bmatrix}
  d_1    \\
  d_2    \\
  d_3    \\
  \vdots \\
  d_n
     
\end{bmatrix} .

For such systems, the solution can be obtained in O(n) operations instead of O(n^3) required by Gaussian elimination. A first sweep eliminates the a_i's, and then an (abbreviated) backward substitution produces the solution. Examples of such matrices commonly arise from the discretization of 1D and natural cubic spline interpolation.

Thomas' algorithm is not stable in general, but is so in several special cases, such as when the matrix is diagonally dominant (either by rows or columns) or symmetric positive definite;

(2025). 9788177587647, Pearson Education India.
for a more precise characterization of stability of Thomas' algorithm, see Higham Theorem 9.12.
(2025). 9780898718027, SIAM.
If stability is required in the general case, Gaussian elimination with (GEPP) is recommended instead.
(2025). 9780898717655, SIAM.


Method
The forward sweep consists of the computation of new coefficients as follows, denoting the new coefficients with primes:

c'_i =
\begin{cases}
 \cfrac{c_i}{b_i},                  & i = 1, \\
 \cfrac{c_i}{b_i - a_i c'_{i - 1}}, & i = 2, 3, \dots, n - 1
\end{cases}
     

and

d'_i =
\begin{cases}
 \cfrac{d_i}{b_i},                                   & i = 1, \\
 \cfrac{d_i - a_i d'_{i - 1}}{b_i - a_i c'_{i - 1}}, & i = 2, 3, \dots, n.
\end{cases}
     

The solution is then obtained by back substitution:

x_n = d'_n,
x_i = d'_i - c'_i x_{i + 1}, \quad i = n - 1, n - 2, \ldots, 1.

The method above does not modify the original coefficient vectors, but must also keep track of the new coefficients. If the coefficient vectors may be modified, then an algorithm with less bookkeeping is:

For i = 2, 3, \dots, n, do

w = \cfrac{a_i}{b_{i-1}},
b_i := b_i-w c_{i-1},
d_i := d_i-w d_{i-1},

followed by the back substitution

x_n = \cfrac{d_n}{b_n},
x_i = \cfrac{d_i - c_i x_{i+1}}{b_i} \quad \text{for } i = n - 1, n - 2, \dots, 1.

The implementation as a C function, which uses scratch space to avoid modifying its inputs for a-c, allowing them to be reused: void thomas(const int X, double xrestrict,

           const double a[restrict X], const double b[restrict X],
           const double c[restrict X], double scratch[restrict X]) {
   /*
    solves Ax = d, where A is a tridiagonal matrix consisting of vectors a, b, c
    X = number of equations
    x[] = initially contains the input, d, and returns x. indexed from [0, ..., X - 1]
    a[] = subdiagonal, indexed from [1, ..., X - 1]
    b[] = main diagonal, indexed from [0, ..., X - 1]
    c[] = superdiagonal, indexed from [0, ..., X - 2]
    scratch[] = scratch space of length X, provided by caller, allowing a, b, c to be const
    not performed in this example: manual expensive common subexpression elimination
    */
   scratch[0] = c[0] / b[0];
   x[0] = x[0] / b[0];
     

   /* loop from 1 to X - 1 inclusive */
   for (int ix = 1; ix < X; ix++) {
       if (ix < X-1){
       scratch[ix] = c[ix] / (b[ix] - a[ix] * scratch[ix - 1]);
       }
       x[ix] = (x[ix] - a[ix] * x[ix - 1]) / (b[ix] - a[ix] * scratch[ix - 1]);
   }
     

   /* loop from X - 2 to 0 inclusive */
   for (int ix = X - 2; ix >= 0; ix--)
       x[ix] -= scratch[ix] * x[ix + 1];
     
}


Derivation
The derivation of the tridiagonal matrix algorithm is a special case of Gaussian elimination.

Suppose that the unknowns are x_1,\ldots, x_n, and that the equations to be solved are:

\begin{alignat}{4}
& && b_1 x_1 && + c_1 x_2 && = d_1 \\ & a_i x_{i - 1} && + b_i x_i && + c_i x_{i + 1} && = d_i \,, \quad i = 2, \ldots, n - 1 \\ & a_n x_{n - 1} && + b_n x_n && && = d_n \,. \end{alignat}

Consider modifying the second (i = 2) equation with the first equation as follows:

(\mbox{equation 2}) \cdot b_1 - (\mbox{equation 1}) \cdot a_2

which would give:

(b_2 b_1 - c_1 a_2) x_2 + c_2 b_1 x_3 = d_2 b_1 - d_1 a_2.

Note that x_1 has been eliminated from the second equation. Using a similar tactic with the modified second equation on the third equation yields:

(b_3 (b_2 b_1 - c_1 a_2) - c_2 b_1 a_3 )x_3 + c_3 (b_2 b_1 - c_1 a_2) x_4
= d_3 (b_2 b_1 - c_1 a_2) - (d_2 b_1 - d_1 a_2) a_3.
     
\,

This time x_2 was eliminated. If this procedure is repeated until the n^{th} row; the (modified) n^{th} equation will involve only one unknown, x_n. This may be solved for and then used to solve the (n - 1)^{th} equation, and so on until all of the unknowns are solved for.

Clearly, the coefficients on the modified equations get more and more complicated if stated explicitly. By examining the procedure, the modified coefficients (notated with tildes) may instead be defined recursively:

\tilde a_i = 0\,

\tilde b_1 = b_1\,
\tilde b_i = b_i \tilde b_{i - 1} - \tilde c_{i - 1} a_i\,

\tilde c_1 = c_1\,
\tilde c_i = c_i \tilde b_{i - 1}\,

\tilde d_1 = d_1\,
\tilde d_i = d_i \tilde b_{i - 1} - \tilde d_{i - 1} a_i.\,

To further hasten the solution process, \tilde b_i may be divided out (if there's no division by zero risk), the newer modified coefficients, each notated with a prime, will be:

a'_i = 0\,

b'_i = 1\,

c'_1 = \frac{c_1}{b_1}\,
c'_i = \frac{c_i}{b_i - c'_{i - 1} a_i}\,

d'_1 = \frac{d_1}{b_1}\,
d'_i = \frac{d_i - d'_{i - 1} a_i}{b_i - c'_{i - 1} a_i}.\,

This gives the following system with the same unknowns and coefficients defined in terms of the original ones above:

\begin{array}{lcl}
x_i + c'_i x_{i + 1} = d'_i \qquad &;& \ i = 1, \ldots, n - 1 \\ x_n = d'_n \qquad &;& \ i = n. \\ \end{array} \,

The last equation involves only one unknown. Solving it in turn reduces the next last equation to one unknown, so that this backward substitution can be used to find all of the unknowns:

x_n = d'_n\,
x_i = d'_i - c'_i x_{i + 1} \qquad ; \ i = n - 1, n - 2, \ldots, 1.


Variants
In some situations, particularly those involving periodic boundary conditions, a slightly perturbed form of the tridiagonal system may need to be solved:

\begin{alignat}{4} & a_1 x_{n} && + b_1 x_1 && + c_1 x_2 && = d_1 \\ & a_i x_{i - 1} && + b_i x_i && + c_i x_{i + 1} && = d_i \,, \quad i = 2, \ldots, n - 1 \\ & a_n x_{n - 1} && + b_n x_n && + c_n x_1 && = d_n \,. \end{alignat}

In this case, we can make use of the Sherman–Morrison formula to avoid the additional operations of Gaussian elimination and still use the Thomas algorithm. The method requires solving a modified non-cyclic version of the system for both the input and a sparse corrective vector, and then combining the solutions. This can be done efficiently if both solutions are computed at once, as the forward portion of the pure tridiagonal matrix algorithm can be shared.

If we indicate by: A=\begin{bmatrix}

  b_1 & c_1 &        &        &  a_1      \\
  a_2 & b_2 & c_2    &        &         \\
      & a_3 & b_3    & \ddots &         \\
      &     & \ddots & \ddots & c_{n-1} \\
  c_n   &     &        & a_n    & b_n
     
\end{bmatrix} ,x= \begin{bmatrix}
  x_1    \\
  x_2    \\
  x_3    \\
  \vdots \\
  x_n
     
\end{bmatrix} ,d= \begin{bmatrix}
  d_1    \\
  d_2    \\
  d_3    \\
  \vdots \\
  d_n
     
\end{bmatrix}

Then the system to be solved is: Ax = d

In this case the coefficients a_1 and c_n are, generally speaking, non-zero, so their presence does not allow to apply the Thomas algorithm directly. We can therefore consider B\in\mathbb{R}^{n\times n} and u,v\in\mathbb{R}^{n} as following: B=\begin{bmatrix}

  b_1-\gamma & c_1 &        &        &  0      \\
  a_2 & b_2 & c_2    &        &         \\
      & a_3 & b_3    & \ddots &         \\
      &     & \ddots & \ddots & c_{n-1} \\
  0   &     &        & a_n    & b_n-\frac{c_na_1}{\gamma}
     
\end{bmatrix}, u=\begin{bmatrix}
  \gamma    \\
  0    \\
  0    \\
  \vdots \\
  c_n
     
\end{bmatrix} ,v= \begin{bmatrix}
  1    \\
  0    \\
  0    \\
  \vdots \\
  a_1/\gamma
     
\end{bmatrix} . Where \gamma\in\mathbb{R} is a parameter to be chosen. The matrix can be reconstructed as A = B + u v^\mathsf{T}. The solution is then obtained in the following way: first we solve two tridiagonal systems of equations applying the Thomas algorithm: By=d \qquad \qquad Bq=u

Then we reconstruct the solution using the Shermann-Morrison formula: \begin{align} x &=A^{-1}d =(B+uv^T)^{-1}d =B^{-1}d-\frac{B^{-1}uv^TB^{-1}}{1+v^TB^{-1}u}d =y-\frac{qv^Ty}{1+v^Tq} \end{align}

The implementation as a C function, which uses scratch space to avoid modifying its inputs for a-c, allowing them to be reused: void cyclic_thomas(const int X, double xrestrict, const double arestrict, const double brestrict, const double crestrict, double cmodrestrict, double urestrict) {

   /*
    solves Ax = v, where A is a cyclic tridiagonal matrix consisting of vectors a, b, c
    X = number of equations
    x[] = initially contains the input v, and returns x. indexed from [0, ..., X - 1]
    a[] = subdiagonal, regularly indexed from [1, ..., X - 1], a[0] is lower left corner
    b[] = main diagonal, indexed from [0, ..., X - 1]
    c[] = superdiagonal, regularly indexed from [0, ..., X - 2], c[X - 1] is upper right
    cmod[], u[] = scratch vectors each of length X
    */
     

   /* lower left and upper right corners of the cyclic tridiagonal system respectively */
   const double alpha = a[0];
   const double beta = c[X - 1];
     

   /* arbitrary, but chosen such that division by zero is avoided */
   const double gamma = -b[0];
     

   cmod[0] = c[0] / (b[0] - gamma);
   u[0] = gamma / (b[0] - gamma);
   x[0] /= (b[0] - gamma);
     

   /* loop from 1 to X - 2 inclusive */
   for (int ix = 1; ix + 1 < X; ix++) {
       const double m = 1.0 / (b[ix] - a[ix] * cmod[ix - 1]);
       cmod[ix] = c[ix] * m;
       u[ix] = (0.0f  - a[ix] * u[ix - 1]) * m;
       x[ix] = (x[ix] - a[ix] * x[ix - 1]) * m;
   }
     

   /* handle X - 1 */
   const double m = 1.0 / (b[X - 1] - alpha * beta / gamma - a[X - 1] * cmod[X - 2]);
   u[X - 1] = (alpha    - a[X - 1] * u[X - 2]) * m;
   x[X - 1] = (x[X - 1] - a[X - 1] * x[X - 2]) * m;
     

   /* loop from X - 2 to 0 inclusive */
   for (int ix = X - 2; ix >= 0; ix--) {
       u[ix] -= cmod[ix] * u[ix + 1];
       x[ix] -= cmod[ix] * x[ix + 1];
   }
     

   const double fact = (x[0] + x[X - 1] * alpha / gamma) / (1.0 + u[0] + u[X - 1] * alpha / gamma);
     

   /* loop from 0 to X - 1 inclusive */
   for (int ix = 0; ix < X; ix++)
       x[ix] -= fact * u[ix];
     
}

There is also another way to solve the slightly perturbed form of the tridiagonal system considered above. Let us consider two auxiliary linear systems of dimension (n-1) \times (n-1) : \begin{align}

\qquad \ \ \ \ \ b_2 u_{2}  + c_2 u_3 &= d_2 \\
a_3 u_2 + b_3 u_3 + c_3 u_4  &= d_3 \\
a_i u_{i-1} + b_i u_i + c_i u_{i+1} &= d_i\\
\dots \\
a_n u_{n - 1}+ b_n u_n\qquad &= d_n \,.
     
\end{align} \quad i = 4, \ldots, n - 1 \qquad \qquad

\begin{align}

\qquad \ \ \ \ \ b_2 v_{2}  + c_2 v_3 &= -a_2 \\
a_3 v_2 + b_3 v_3 + c_3 v_4  &= 0 \\
a_i u_{i-1} + b_i u_i + c_i u_{i+1} &= 0\\
\dots \\
a_n v_{n - 1}+ b_n v_n\qquad &= -c_n \,.
     
\end{align} \quad i = 4, \ldots, n - 1

For convenience, we additionally define u_1 = 0 and v_1 = 1. We can now find the solutions \{u_2,u_3\dots,u_n \} and \{v_2,v_3\dots,v_n \} applying Thomas algorithm to the two auxiliary tridiagonal system.

The solution \{ x_1,x_2\dots,x_n \} can be then represented in the form: x_i = u_i + x_1 v_i \qquad i=1,2,\dots, n

Indeed, multiplying each equation of the second auxiliary system by x_1, adding with the corresponding equation of the first auxiliary system and using the representation x_i = u_i + x_1 v_i, we immediately see that equations number through of the original system are satisfied; it only remains to satisfy equation number . To do so, consider formula for i=2 and i=n and substitute x_2 = u_2 + x_1 v_2and x_n = u_n + x_1 v_n into the first equation of the original system. This yields one scalar equation for x_1: b_1x_1+c_1(u_2+x_1v_2)+a_1(u_n+x_1v_n) = d_1

As such, we find: x_1 = \frac{d_1-a_1u_n-c_1u_2}{b_1+a_1v_n+c_1v_2}

The implementation as a C function, which uses scratch space to avoid modifying its inputs for a-c, allowing them to be reused: void cyclic_thomas(const int X, double xrestrict, const double arestrict, const double brestrict, const double crestrict, double cmodrestrict, double vrestrict) {

   /* first solve a system of length X - 1 for two right hand sides, ignoring ix == 0 */
   cmod[1] = c[1] / b[1];
   v[1] = -a[1] / b[1];
   x[1] = x[1] / b[1];
     

   /* loop from 2 to X - 1 inclusive */
   for (int ix = 2; ix < X - 1; ix++) {
       const double m = 1.0 / (b[ix] - a[ix] * cmod[ix - 1]);
       cmod[ix] = c[ix] * m;
       v[ix] = (0.0f  - a[ix] * v[ix - 1]) * m;
       x[ix] = (x[ix] - a[ix] * x[ix - 1]) * m;
   }
     

   /* handle X - 1 */
   const double m = 1.0 / (b[X - 1] - a[X - 1] * cmod[X - 2]);
   cmod[X - 1] = c[X - 1] * m;
   v[X - 1] = (-c[0]    - a[X - 1] * v[X - 2]) * m;
   x[X - 1] = (x[X - 1] - a[X - 1] * x[X - 2]) * m;
     

   /* loop from X - 2 to 1 inclusive */
   for (int ix = X - 2; ix >= 1; ix--) {
       v[ix] -= cmod[ix] * v[ix + 1];
       x[ix] -= cmod[ix] * x[ix + 1];
   }
     

   x[0] = (x[0] - a[0] * x[X - 1] - c[0] * x[1]) / (b[0] + a[0] * v[X - 1] + c[0] * v[1]);
     

   /* loop from 1 to X - 1 inclusive */
   for (int ix = 1; ix < X; ix++)
       x[ix] += x[0] * v[ix];
     
}

In both cases the auxiliary systems to be solved are genuinely tri-diagonal, so the overall computational complexity of solving system Ax = d remains linear with the respect to the dimension of the system , that is O(n) arithmetic operations.

In other situations, the system of equations may be block tridiagonal (see ), with smaller submatrices arranged as the individual elements in the above matrix system (e.g., the 2D Poisson problem). Simplified forms of Gaussian elimination have been developed for these situations.

(2025). 9783540346586, Springer, New York.

The textbook Numerical Mathematics by , Sacco and Saleri, lists a modified version of the algorithm which avoids some of the divisions (using instead multiplications), which is beneficial on some computer architectures.

Parallel tridiagonal solvers have been published for many vector and parallel architectures, including GPUs

For an extensive treatment of parallel tridiagonal and block tridiagonal solvers see

(2025). 9789401771887, Springer.

Page 1 of 1
1
Page 1 of 1
1

Account

Social:
Pages:  ..   .. 
Items:  .. 

Navigation

General: Atom Feed Atom Feed  .. 
Help:  ..   .. 
Category:  ..   .. 
Media:  ..   .. 
Posts:  ..   ..   .. 

Statistics

Page:  .. 
Summary:  .. 
1 Tags
10/10 Page Rank
5 Page Refs